/**
 * @project name:mail
 */
package com.situ.mail.shop.service.impl;

import com.situ.mail.base.BaseMapper;
import com.situ.mail.base.BaseServiceImpl;
import com.situ.mail.shop.dao.UserMapper;
import com.situ.mail.shop.pojo.User;
import com.situ.mail.shop.service.UserService;
import com.situ.mail.utils.ConfigUtil;
import com.situ.mail.utils.MD5Util;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Author : yun&bo
 * @CreateDate : ==>2020/12/24 10:12
 * @ClassName : ==>
 * @Description : ==>
 * @Version
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    private UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Override
    public BaseMapper<User> getBaseMapper() {
        return userMapper;
    }


    /**
     * @PostConstruct 此注释修饰的方法，相当于Spring容器中的init-mthod 随着项目启动自运行
     * @Title: initUserData
     * @Description: TOD
     * @Return: 方法随系统的启动而自运行
     */
    @PostConstruct
    @Override
    public void initUserData() {
        Integer userCount = userMapper.selectUserCount();
        if (userCount == null || userCount == 0) {
            //此处应该自动的初始化一条用户数据
            User user = new User();
            user.setUserName("管理员");
            user.setUserCode("admin");
            user.setUserType(1);
            user.setUserPass(MD5Util.encode("123456"));
            user.setIsLock(User.IS_LOCK_NO);
            user.setActiveFlag(1);
            user.setCreateBy("admin");
            user.setCreateDate(new Date());
            userMapper.insert(user);
        }
    }

    /**
     * @param user
     * @return 1 成功  0 失败
     */
    @Override
    public int doAddUser(User user) {
        user.setUserType(User.NOT_IS_ADMIN);
        user.setUserPass(MD5Util.encode(user.getUserPass()));
        user.setIsLock(User.IS_LOCK_NO);
        user.setActiveFlag(1);
        user.setCreateBy("admin");
        user.setCreateDate(new Date());
        return userMapper.insert(user);
    }


    @Override
    public int findUserByCode(String userCode) {

        return userMapper.findUserByCode(userCode);
    }


    /**
     * 查到该实例并 放入session
     * <p>
     * 返回值  0  没有该用户   1 登录成功    2 用户被锁定
     *
     * @param user
     * @return
     */
    @Override
    public User findUserByUser(User user) {
        return null;
    }

    @Override
    public User selectByUserCodeAndUserPass(User user) {
        return userMapper.selectByUserCodeAndUserPass(user);
    }

    @Override
    public int exitLogin(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute(ConfigUtil.SESSION_LOGIN_USER_NAME);
        Cookie cookie = new Cookie(ConfigUtil.SESSION_LOGIN_USER_NAME, "");
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return 1;
    }


}
